package com.xinke.module.system.service.sms;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.sms.*;
import com.xinke.module.system.model.entity.sms.SmsChannelDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.sms.SmsChannelMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 短信渠道 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class SmsChannelServiceImpl implements SmsChannelService {

    @Resource
    private SmsChannelMapper smsChannelMapper;

    @Override
    public Long createSmsChannel(SmsChannelSaveReqVO createReqVO) {
        // 插入
        SmsChannelDO smsChannel = BeanUtils.toBean(createReqVO, SmsChannelDO.class);
        smsChannelMapper.insert(smsChannel);
        // 返回
        return smsChannel.getChannelId();
    }

    @Override
    public void updateSmsChannel(SmsChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateSmsChannelExists(updateReqVO.getChannelId());
        // 更新
        SmsChannelDO updateObj = BeanUtils.toBean(updateReqVO, SmsChannelDO.class);
        smsChannelMapper.updateById(updateObj);
    }

    @Override
    public void deleteSmsChannel(Long channelId) {
        // 校验存在
        validateSmsChannelExists(channelId);
        // 删除
        smsChannelMapper.deleteById(channelId);
    }

    private void validateSmsChannelExists(Long channelId) {
        if (smsChannelMapper.selectById(channelId) == null) {
            throw exception(null);
        }
    }

    @Override
    public SmsChannelDO getSmsChannel(Long channelId) {
        return smsChannelMapper.selectById(channelId);
    }

    @Override
    public PageResult<SmsChannelDO> getSmsChannelPage(SmsChannelPageReqVO pageReqVO) {
        return smsChannelMapper.selectPage(pageReqVO);
    }

}